<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: DataType.XML.format()</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: DataType.XML.format()</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><style scoped>
/* custom styles for this example */
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;}
</style>

<div class="intro">
    <p>The XML module of the DataType Utility allows you to take an XML document and convert it to a string.</p>
</div>

<div class="example yui3-skin-sam">
    <form id="demo">
    <input type="button" id="demo_btn" value="Show XML as a string">
    <div id="demo_output" class="output">&nbsp;</div>
</form>

<script>
YUI().use("node", "datatype-xml", function (Y) {
    Y.on("click", function(e){
        var myXMLDoc = Y.DataType.XML.parse("<myroot><item type='foo'><name>Abc</name><rank>1</rank></item><item type='bar'><name>Def</name><rank>2</rank></item><item type='bat'><name>Ghhi</name><rank>3</rank></item></myroot>"),
            output = Y.DataType.XML.format(myXMLDoc);

        if(output === "") {
            output += "(Invalid XML document)"
        }
        Y.one("#demo_output").setContent(output.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"));
    }, "#demo_btn");
});
</script>

</div>

<p>To output an XML document as a string, simply call the <code>format()</code> function of the DataType.XML class:</p>

<pre class="code prettyprint">YUI().use(&quot;datatype-xml&quot;, function(Y) {
    var xmlString =
        &#x27;&lt;myroot&gt;&#x27; +
            &#x27;&lt;item type=&quot;foo&quot;&gt;&#x27; +
                &#x27;&lt;name&gt;Abc&lt;&#x2F;name&gt;&#x27; +
                &#x27;&lt;rank&gt;1&lt;&#x2F;rank&gt;&#x27; +
            &#x27;&lt;&#x2F;item&gt;&#x27; +
            &#x27;&lt;item type=&quot;bar&quot;&gt;&#x27; +
                &#x27;&lt;name&gt;Def&lt;&#x2F;name&gt;&#x27; +
                &#x27;&lt;rank&gt;2&lt;&#x2F;rank&gt;&#x27; +
            &#x27;&lt;&#x2F;item&gt;&#x27; +
            &#x27;&lt;item type=&quot;bat&quot;&gt;&#x27; +
                &#x27;&lt;name&gt;Ghhi&lt;&#x2F;name&gt;&#x27; +
                &#x27;&lt;rank&gt;3&lt;&#x2F;rank&gt;&#x27; +
            &#x27;&lt;&#x2F;item&gt;&#x27; +
        &#x27;&lt;&#x2F;myroot&gt;&#x27;;

    var myXMLDoc = Y.DataType.XML.parse(xmlString);

    alert(Y.DataType.XML.format(myXMLDoc));
});</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="Formatting dates into strings.">
                                        <a href="datatype-dateformat.html">DataType.Date.format()</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Formatting dates into strings using pre-packaged language resource bundles.">
                                        <a href="datatype-dateformat-lang.html">Formatting Dates Using Language Resource Bundles</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Parsing data into dates.">
                                        <a href="datatype-dateparse.html">DataType.Date.parse()</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Formatting numbers into strings.">
                                        <a href="datatype-numberformat.html">DataType.Number.format()</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Parsing data into numbers.">
                                        <a href="datatype-numberparse.html">DataType.Number.parse()</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Formatting XML documents into strings.">
                                        <a href="datatype-xmlformat.html">DataType.XML.format()</a>
                                    </li>
                                
                            
                                
                                    <li data-description="Parsing strings into XML documents.">
                                        <a href="datatype-xmlparse.html">DataType.XML.parse()</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
